#include <windows.h>

const char* get_text_from_clipboard()
{
    if (!(IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(NULL)))
        return "";
    HANDLE hData = GetClipboardData(CF_TEXT);
    const char* pGlobal = (char*)GlobalLock(hData);
    GlobalUnlock(hData);
    CloseClipboard();
    return pGlobal;
}

bool copy_text_to_clipboard(const char *pstr)
{
    int len = 0;
    while (pstr[len++]) ;
    HGLOBAL hwnd;
    if (!(OpenClipboard(NULL) && EmptyClipboard() && (hwnd = GlobalAlloc(GMEM_DDESHARE, len))))
        return false;
    char *pGlobal = (char*)GlobalLock(hwnd);
    len = 0;
    while (pGlobal[len] = pstr[len])
        len++;
    //strcpy(pGlobal, LPCTSTR(pstr));
    GlobalUnlock(hwnd);
    SetClipboardData(CF_TEXT, pGlobal);
    CloseClipboard();
    return true;
}

//bool  copy_text_to_clipboard(const char *str)
//{
//    int len = 0;
//    while (str[len++] != '\0') ;
//    HGLOBAL hGlobal = GlobalAlloc(GHND | GMEM_SHARE, len);
//    if (!(hGlobal && OpenClipboard(NULL) &&EmptyClipboard()))
//        return false;
//
//    char *pGlobal = (char*)GlobalLock(hGlobal);
//    while (*pGlobal++ = *str++) ;
//    GlobalUnlock(hGlobal);
//    SetClipboardData(CF_TEXT, pGlobal - len);
//    CloseClipboard();
//    return true;
//}
